<?php
class  Admin_login extends CI_Model
{
        function __construct()
        {
                parent::__construct();
        }
        function get_post()
        {
                $sub=$this->input->post('submit');
                if(isset($sub)){
                        $usrname=$this->input->post('usrname');
                        $pwd=$this->input->post('pwd');
                        $pwd=md5(md5($pwd));
                        $sql="select `uid`,`permit` from admin where `admin_name`='$usrname' && `admin_pwd`='$pwd'";
                        $query=$this->db->query($sql);
                        log_message("debug", "user:".$usrname." passwd:".$pwd." query num:".$query->num_rows());
                        if($query->num_rows() > 0)
                        {
                                $msg=$query->row_array();
                                if($msg['permit']==1)
                                {//超级管理员
                                        $this->session->set_userdata('admin_name',$usrname);
                                        redirect(site_url('welcome/admin_cms/'), 'refresh ', 302);
                                }
                                elseif($msg['permit']==0)
                                {//普通管理员
                                        $this->session->set_userdata('name',$usrname);
                                        redirect(site_url('welcome/upload_cms/'), 'refresh ', 302);
                                }
                                elseif($msg['permit']==3)
                                {//只能浏览
                                        $this->session->set_userdata('scan_name',$usrname);
                                        redirect(site_url('scan_admin/scan_cms/'), 'refresh ', 302);
                                }
                        }
                        else
                        {
                                return false;
                        }
                }
                else//防止翻墙
            {
                redirect(site_url('welcome/index/'), 'refresh ', 302);
            }

        }
        function insert_admin()
        {
                $sub=$this->input->post('submit');
                if(isset($sub)){
                        $usrname=$this->input->post('usrname');
                        $pwd=$this->input->post('pwd');
                        $pwd=md5(md5($pwd));
                        $data = array(
                           'uid' =>'0',
                        'admin_name' => $usrname,
             'admin_pwd' => $pwd,
            );
                        if($this->db->insert('admin', $data))
                        {
                                $data['msg']=array('title'=>'Admin','redirect'=>'welcome/add_admin');
                                $this->load->view('add_success',$data);
                        }
                }
                else
            {
                redirect(site_url('welcome/add_admin/'), 'refresh ', 302);
            }
        }
        function select_admin()
        {
                $sql="select * from `admin`";
                $query=$this->db->query($sql);
                if ($query->num_rows() > 0)
                {
                        return $query->result_array();
                }
                else
                {
                        return FALSE;
                }
        }
        function select_message($uid)
        {
                $sql="select `uid`,`admin_name` from admin where `uid`='$uid'";
                $query=$this->db->query($sql);
                $row=$query->row_array();
                return $row;
        }
        function update_admin()
        {
                $sub=$this->input->post('submit');
                if(isset($sub)){
                        $uid=$this->input->post('uid');
                        $update['admin_name']=$this->input->post('usrname');
                        $pwd=$this->input->post('pwd');
                        $update['admin_pwd']=md5(md5($pwd));
                        $this->db->where('uid',$uid);
                        if($this->db->update('admin',$update))
                        {
                                redirect(site_url('welcome/manage_admin/'),'refresh',302);
                        }
                }
                else//防止翻墙
            {
                redirect(site_url('welcome/update/'), 'refresh ', 302);
            }
        }
        function delete_admin($uid)
        {
                $this->db->where('uid', $uid);
                if($this->db->delete('admin'))
                {
                        redirect(site_url('welcome/manage_admin/'),'refresh',302);
                }
        }
        function insert_category()
        {
                $sub=$this->input->post('sub');
                if(isset($sub))
                {
                        $cate=$this->input->post('cate');
                        $sql="insert into `classify`(`type`,`tree`)values('$cate',1)";
                        if($this->db->query($sql))
                        {
                                $data['msg']=array('title'=>'Category','redirect'=>'welcome/add_category');
                                $this->load->view('add_success',$data);
                        }
                }else
                        {
                                redirect(site_url('welcome/add_category'), 'refresh ', 302);
                        }
        }
        function insert_second_category()
        {
                $sub=$this->input->post('sub');
                if(isset($sub))
                {
                        $fid=$this->input->post('fid');
                        if($fid!='')
                        {
                                $second_cate=$this->input->post('second_cate');
                                $sql="insert into `classify`(`type`,`fid`,`tree`)values('$second_cate','$fid',2)";
                                if($this->db->query($sql))
                                {
                                        return true;
                                        //redirect(site_url('welcome/add_second_category'), 'refresh ', 302);
                                }
                                else
                                {
                                        return false;
                                }
                        }
                }
        }

}
















